from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.factors import BollingerBands, RSI
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.pipeline.classifiers.morningstar import Sector
from alphalens.utils import get_clean_factor_and_forward_returns
from alphalens.tears import create_full_tear_sheet
from alphalens.tears import create_returns_tear_sheet
from alphalens.tears import create_information_tear_sheet
def make_pipeline():
base_universe = QTradableStocksUS()
rsi = RSI(inputs=[USEquityPricing.close],
window_length=14,
mask=base_universe)
bb = BollingerBands(inputs=[USEquityPricing.close],
window_length=14,
mask=base_universe,
k=2)
factor_to_analyze = rsi+bb.upper-bb.lower
sector = Sector()
return Pipeline(
columns={'factor_to_analyze': factor_to_analyze, 'sector': sector},
screen=QTradableStocksUS() & factor_to_analyze.notnull() & sector.notnull()
)
factor_data = run_pipeline(make_pipeline(), '2015-1-1', '2016-1-1')
pricing_data = get_pricing(factor_data.index.levels[1], '2015-1-1', '2016-6-1', fields='open_price')
longest_look_forward_period = 63 # week = 5, month = 21, quarter = 63, year = 252
range_step = 5
merged_data = get_clean_factor_and_forward_returns(
factor=factor_data['factor_to_analyze'],
prices=pricing_data,
periods=range(1, longest_look_forward_period, range_step)
)
create_full_tear_sheet(merged_data)